cap log close
log using "cr-mrp-data.log", replace

	// Load IAT data set
	use "$data/iat", clear

		// Keep only states with teacher data
			preserve
				use "$data/teacher_synth_county", clear
				keep fips
				duplicates drop
				tempfile tchsynth
				save `tchsynth'
			restore
			merge m:1 fips using `tchsynth', assert(1 3) keep(3) nogen
		
		// Create county-level teacher flags
		foreach var in imp exp {
			gen flag`var' = !mi(`var'licitbias) & k12==1 & !mi(raceomb)
			bys countyid: egen tot_tch_`var' = total(flag`var')
			drop flag`var'
			}
		
		keep countyid tot_*
		duplicates drop
	
		// Merge with teacher MRP scores
		merge 1:1 countyid using "$data/mrp_tch_scores.dta", keep(1 3) nogen keepusing(mrp*)
		
		rename mrp1_imp_bias tch_imp_bias
		rename mrp1_exp_bias tch_exp_bias
		
		tempfile tchmrp
		save `tchmrp'
		
	// Load IAT dataset
	use "$data/iat", clear
	
		// Create county-level teacher flags
		foreach var in imp exp {
			gen flag`var' = !mi(`var'licitbias) & !mi(raceomb) & !mi(sex) & !mi(age)
			bys countyid: egen tot_`var' = total(flag`var')
			drop flag`var'
			}
		
		keep countyid tot_*
		duplicates drop
		
		// Merge with pooled MRP scores
		merge 1:1 countyid using "$data/mrp_all_scores.dta", keep(1 3) nogen keepusing(mrp*)

		rename mrp1_imp_bias all_imp_bias
		rename mrp1_exp_bias all_exp_bias
		
		merge 1:1 countyid using `tchmrp', nogen
		
	// Merge with unadjusted scores - replace missing those without data
	merge 1:1 countyid using "$data/bias_data", assert(3) nogen
	replace all_imp_bias = . if mi(all_co_impbias)
	replace all_exp_bias = . if mi(all_co_expbias)
	replace tch_imp_bias = . if mi(tch_co_impbias)
	replace tch_exp_bias = . if mi(tch_co_expbias)
		
		// Compare to Hoover's scores
		preserve
			insheet using "$raw/gefpd_study_3_county_bias_estimates_all_methods.csv", comma clear
			keep county_fips mrp1imp_bias mrp1exp_bias
			rename county_fips countyid
			isid countyid
			destring *bias, replace force
			tempfile temp
			save `temp'
		restore
		merge 1:1 countyid using `temp', keep(1 3) nogen
		
		pwcorr all_imp_bias all_co_impbias mrp1imp_bias // Close to hoovers
		pwcorr all_exp_bias all_co_expbias mrp1exp_bias
		
		keep countyid tot_imp tot_exp all_imp_bias all_exp_bias tch_imp_bias tch_exp_bias tch_co_impbias tch_co_expbias
		
		pwcorr tch_imp_bias tch_co_impbias
		pwcorr tch_exp_bias tch_co_expbias
		drop tch_co_impbias tch_co_expbias
		
	save "$data/mrp_data", replace
		
log close
		
